package com.etlmaster.executor.steps.rowprocessor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.etlmaster.executor.Step;
import com.etlmaster.executor.StepMeta;
import com.etlmaster.executor.annotation.StepInstruction;
import com.etlmaster.executor.utils.StepState;

import freemarker.core.FreeMarkerTemplate;
/**
 * @author Larry
 * @copyright Larry@etlMaster
 * @since 2017-09-10
 */
@StepInstruction("rowProcessJdbcReader")


public class JdbcReaderStep extends Step{


	private JdbcReaderStepMeta meta;
	public JdbcReaderStep(StepMeta meta) {
		super(meta);
		this.meta=(JdbcReaderStepMeta) meta;
	}

	@Override
	protected int handle() {
		Map<String,String> map=new HashMap<String,String>();
		map.put("type", "reader");
		map.put("id", meta.getId());
		map.put("name", meta.getName());
		map.put("code", meta.getId());
		map.put("class", "com.etlmaster.rowprocessor.steps.JdbcReader");
		StringBuilder sb=new StringBuilder();
		sb.append("{\"dbName\":\"")
		.append(meta.getDbName())
		.append("\",\"sqlExpression\":\"")
		.append(meta.getSqlExpression())
		.append("\",\"tableName\":\"")
		.append(meta.getTableName())
		.append("\"}");
		map.put("param", sb.toString());
		String rowstep=FreeMarkerTemplate.getFpl("plugin",map);
		List<String> rowSteps=new ArrayList<String>();
		rowSteps.add(rowstep);
		meta.getStepState().getReturnValue().put("jobJson", rowSteps);
		meta.log("INFO","生成{}配置成功",meta.getName());
		return StepState.RUNNING_SUCCESSED;
	}

	@Override
	public boolean stop() {
		// TODO Auto-generated method stub
		return false;
	}

}
